package com.platform.data.service.impl;

import com.platform.data.model.ReadDataModel;
import com.platform.data.model.WriteDataModel;
import com.platform.data.service.ProcessDataService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 处理数据
 *
 * @author weijun.zhu
 * @date 2025/5/11 18:19
 */
public class ProcessDataServiceImpl implements ProcessDataService {
    private static final Logger log = LoggerFactory.getLogger(ProcessDataServiceImpl.class);

    /**
     * 组装数据
     *
     * @param readDataList readDataList
     * @return {@link List<WriteDataModel>}
     */
    @Override
    public List<WriteDataModel> precessData(List<ReadDataModel> readDataList) {
        List<WriteDataModel> writeDataList = new ArrayList<>();

        // 按照属性名称分组
        Map<String, List<ReadDataModel>> readModelMap = new HashMap<>();
        ReadDataModel firstSheet = null;
        for (ReadDataModel model : readDataList) {
            String fieldName = model.getFieldName();
            List<ReadDataModel> modelList = readModelMap.getOrDefault(fieldName, new ArrayList<>());
            modelList.add(model);
            readModelMap.put(fieldName, modelList);

            if(Boolean.TRUE.equals(model.getFirstSheetFlag())){
                firstSheet = model;
            }
        }

        if(Objects.isNull(firstSheet)){
            log.error("ProcessDataServiceImpl.precessData|解析文件未读取到第一个Sheet页数据内容，请检查！");
            return writeDataList;
        }

        // 第一个Sheet页数据
        List<Map<String, Object>> dataList = firstSheet.getDataList();
        for (Map<String, Object> dataMap : dataList) {
            String uniqueFieldName = firstSheet.getUniqueFieldName();
            Object uniqueValue = dataMap.get(uniqueFieldName);
            if(Objects.nonNull(uniqueValue)){
                String childNodeName = firstSheet.getChildNodeName();
                if(StringUtils.isNotBlank(childNodeName)){
                    dataMap.put(childNodeName, recursiveLoopDataModel(uniqueFieldName, uniqueValue, childNodeName, readModelMap));
                }
            }

            WriteDataModel writeModel = new WriteDataModel();
            writeModel.setStationId(String.valueOf(uniqueValue));
            writeModel.setStationNodeName(firstSheet.getFieldName());
            writeModel.setBodyModel(dataMap);
            writeDataList.add(writeModel);
        }
        return writeDataList;
    }

    /**
     * 循环递归，组装关联设备信息
     *
     * @param superFieldName superFieldName
     * @param superFileValue superFileValue
     * @param nodeName nodeName
     * @param readModelMap readModelMap
     * @return {@link List<Map<String, Object>>}
     */
    private List<Map<String, Object>> recursiveLoopDataModel(String superFieldName, Object superFileValue,
                                                             String nodeName, Map<String, List<ReadDataModel>> readModelMap) {
        List<ReadDataModel> modelList = readModelMap.get(nodeName);
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (ReadDataModel dataModel : modelList) {
            List<Map<String, Object>> modelDataList = dataModel.getDataList();
            for (Map<String, Object> dataModelMap : modelDataList) {
                Object superValue = dataModelMap.get(superFieldName);
                if(!Objects.equals(superFileValue, superValue)){
                    continue;
                }
                dataModelMap.remove(superFieldName);

                String uniqueFieldName = dataModel.getUniqueFieldName();
                Object uniqueValue = dataModelMap.get(uniqueFieldName);
                if(Objects.nonNull(uniqueValue)){
                    String childNodeName = dataModel.getChildNodeName();
                    if(StringUtils.isNotBlank(childNodeName)){
                        dataModelMap.put(childNodeName, recursiveLoopDataModel(uniqueFieldName, uniqueValue, childNodeName, readModelMap));
                    }
                }
                dataList.add(dataModelMap);
            }
        }
        return dataList;
    }
}
